/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * <p>Project: jse2302 - TreeMapDemo
 * <p>Powered by webrx On 2023-06-26 09:53:23
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap<String, String> tss = new TreeMap<>();

        //Map<Integer,String> tm = new HashMap<>(); 无序
        //TreeMap<Integer,String> tm = new TreeMap<>(); //根据key自然顺序
        TreeMap<Integer, String> tm = new TreeMap<>((a, b) -> b - a); //根据key降序
        tm.put(66, "c");
        tm.put(10, "java");
        tm.put(5, "go");
        tm.put(50, "python");
        //System.out.println(tm);

        TreeMap<String, Integer> tm2 = new TreeMap<>((a, b) -> b.length() - a.length());
        tm2.put("c", 66);
        tm2.put("java", 10);
        tm2.put("go", 5);
        tm2.put("python", 50);
        tm2.put("c++", 150);
        tm2.put("javascript", 11);
        System.out.println(tm2);

        TreeMap<User,Integer> tm3 = new TreeMap<>((a,b)->b.getId()-a.getId());
        User ls1 = new User(10,"李四");
        User ls2 = new User(5,"李四");
        User ls3 = new User(1,"李四");
        User ls4 = new User(15,"李四");
        tm3.put(ls1,1000);
        tm3.put(ls2,200);
        tm3.put(ls3,3000);
        tm3.put(ls4,300);

        System.out.println(tm3.size());
        System.out.println(tm3);

        System.out.println("-------------");
        System.out.println(tm3.lastEntry());
        System.out.println(tm3.lastEntry().getKey());
        System.out.println(tm3.lastEntry().getValue());

        //根据key取value
        System.out.println(tm3.get(ls1));

        Set<Map<String,Object>> ss = new HashSet<>();
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("id",100);
        map.put("name","李四");
        list.add(map);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",200);
        map2.put("name","张三丰");
        map2.put("address","河南省郑州市");
        list.add(map2);
        list.add(map2);
        list.add(map2);
        list.add(map2);
        list.add(map2);

        System.out.println(list.size());
        System.out.println(list);

    }
}
